package com.wxxymaker.controller;

import com.wxxymaker.model.entity.Stage;
import com.wxxymaker.service.StageCountService;
import com.wxxymaker.service.StageService;
import com.wxxymaker.utils.FileUtil;
import com.wxxymaker.utils.web.ResponseWarp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.util.Map;
import java.util.Properties;

/**
 * @Author: Soft
 * @Date: 2018/5/9 17:04
 * @Desc: 阶段统计控制器
 */
@Controller
@RequestMapping("/stageCount")
public class StageCountHandler {

    @Autowired
    private StageCountService stageCountService;

    @Autowired
    private StageService stageService;

    /**
     * 重新统计该阶段的记录，并且更新整个公司的总评
     * @param stage 阶段包涵 id和type
     * @return 请求结果
     */
    @PostMapping("/recount")
    @ResponseBody
    public ResponseWarp reCount(Stage stage) {
        stage.extend(stageService.getOne(stage.getId()));
        stageCountService.countScoreAndSave(stage);
        return ResponseWarp.ok();
    }

    @PostMapping("/update/standard")
    @ResponseBody
    public ResponseWarp updateStandard(String one, String twe, String three, String four) throws IOException {
        File props = FileUtil.getPropsFile();
        Properties ps = new Properties();
        ps.put("one", one);
        ps.put("twe", twe);
        ps.put("three", three);
        ps.put("four", four);
        ps.store(new FileWriter(props), "Statistical score criteria");
        return ResponseWarp.ok();
    }
}
